


************************************************************;
** Create Matched HEDIS2004 matched with Denominator file **;
************************************************************;

options ls=76 nofmterr;

libname raw '/data/Medicare_P01_2009/data/HEDIS/rawdata';
libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata';
libname plan '/data/Medicare_P01_2009/data/HEDIS/plandata';


data a01; set raw.hmoid04;

     proc sort nodupkey; by sub_id;

data a02(drop=hic_num m_month); set raw.hedis2004data;

     hic_number=hic_num;
     m_months = m_month;

     proc sort; by sub_id;


data a; merge a01 a02(in=ok); by sub_id;

     if ok;   

     length hic_org $ 18 ;
     hic_org=org_id||'-'||hic_number;

     ** Invalid HIC number Indicators **;

     if substr(hic_number,2,11)=' ' or
        substr(hic_number,3,10)=' ' or 
        substr(hic_number,4,9) =' ' or
        substr(hic_number,5,8) =' ' then short_hic=1; else short_hic=0;

      if hic_number=' ' or
         hic_number='00000' or
         hic_number='000000' or
         hic_number='0000000' or
         hic_number='00000000' or
         hic_number='000000000' or
         hic_number='0000000000' or
         hic_number='00000000000' or
         hic_number='000000000000' or

         hic_number='99999' or
         hic_number='999999' or
         hic_number='9999999' or
         hic_number='99999999' or
         hic_number='999999999' or
         hic_number='9999999999' or
         hic_number='99999999999' or
         hic_number='999999999999' or  
         hic_number='Error'   or
         hic_number='UNKNOWN' then invalid_hic=1; else invalid_hic=0; 

         proc freq;
              tables short_hic invalid_hic;
              title 'HEDIS2004 HIC Number of M_Months Check';
    
  data a001; set a;

       **** Exclude Invalid Cases ****;

       if short_hic=1   then delete;
       if invalid_hic=1 then delete;

       proc freq;
            tables m_months;


   data a0; set a001;

       if m_months=0 then delete;
     
       proc sort; by hic_org;

    data a1; set a0;

         proc means n sum nway noprint;
              class hic_org;
              var m_months;
              output out=totmon0 sum=totmonth;

    data totmon; set totmon0;
         
         keep hic_org totmonth;
        
         proc sort; by hic_org;


       *** Add Plan Type Information ***;

      data plan0; set plan.hedis2004planinfo;
           org_id    = hcfa_contract_number ;
           plan_type = general_11;

        data plan; set plan0;
             keep org_id plan_type;
             proc sort; by org_id;
    

    data a00; merge a0 totmon; by hic_org;
          
           proc sort; by org_id;
 
    data a002; merge a00(in=ok) plan; by org_id;

         if ok;

         proc freq;
              tables totmonth;


    data a2; set a002;

         if totmonth>12 then delete; /** drop cases with more than 1 year months in one contract **/
         id_num=_n_;

         drop short_hic invalid_hic;

         proc sort; by hic_number;


     *** Outpupt LINKID2004 ***;

/**
     data raw.linkid2004; set a2;

          keep hic_number id_num;

*** Already output last run **/

     
data denom; set new.hedis04denom;

     proc sort; by hic_number;

data new.hedis2004; merge a2(in=ok) denom; by hic_number;

     if sub_id=6626 then org_id='HS524';
     if sub_id=6628 then org_id='HN524';

     if denom_flag=. then denom_flag=0;     

     proc freq;
          tables totmonth denom_flag plan_type;

    *proc contents varnum;

endsas;

